"use strict";

/**
 * @auther wtfssd
 * Created by wtfssd on 2017/10/27
 */
import * as React from 'react'
import {TextProperties, ViewProperties, ViewStyle, Linking} from "react-native";
import {urlReg} from "../constant";
import {commonStyles} from "../assets/styles/common";

const {Text} = require('react-native');

interface MyTextProps extends TextProperties {
    color?: string,
    fontSize?: number ,
    fontWeight?: string,
    style?: ViewStyle | object,
}

class MyText extends React.Component<MyTextProps, any> {
    static defaultProps = {
        allowFontScaling: false,
        color: '#333',
        fontSize: 14,
        fontWeight: 'normal'
    };

    constructor(props: MyTextProps, context: any) {
        super(props, context);

    }

    static getStyles(obj) {
        let style = {};
        if(!obj)return style;

        if(obj.color){
            style['color'] = obj.color;
        }
        if(obj.fontSize){
            style['fontSize'] = obj.fontSize;
        }
        if(obj.fontWeight){
            style['fontWeight'] = obj.fontWeight;
        }

        return style;
    }

    static getProps(obj){
        let props={};

        props['style'] = MyText.getStyles(obj);

        if(!obj)return props;


        if(obj.numberOfLines){
            props['numberOfLines'] = obj.numberOfLines;
        }
        if(obj.allowFontScaling){
            props['allowFontScaling'] = obj.allowFontScaling;
        }

        return props;
    }

    static onTextPress(url) {
        Linking.canOpenURL(url).then(supported => {
            if (!supported) {
                console.log('Can\'t handle url: ' + url);
            } else {
                return Linking.openURL(url);
            }
        }).catch(err => console.error('An error occurred', err));
    };

    static checkLinks(data, result, props = null) {
        if (typeof data === 'string'||typeof data === 'number') {
            const _d= data+'';
            const res = _d.split(urlReg);
            const res1 = _d.match(urlReg);
            if (res1) {
                let index = 0;
                res.map((item, i) => {
                    if (item.indexOf('http') !== -1 && res1) {
                        result.push(<Text key={i} style={commonStyles.linkText}
                                          onPress={MyText.onTextPress.bind(null, res1[index])}>{res1[index]}</Text>);
                        index = index + 1;
                    } else {
                        result.push(item);
                    }
                });
            } else {
                result.push(data);
            }

        }
        else if (Array.isArray(data)) {
            data.map(item => {
                MyText.checkLinks(item, result, );
            })
        } else if (typeof data === 'object') {

            MyText.checkLinks(data.props.children, result);
        }
        return result;
    }


    render() {

        const style = [{
            color: this.props.color,
            fontSize: this.props.fontSize,
            fontWeight: this.props.fontWeight,
            backgroundColor:'transparent'
        }, this.props.style];

        return (
            <Text  {...this.props} style={style}>{MyText.checkLinks(this.props.children, [])}</Text>
        );
    }
}


export default MyText;